<template>
    <el-container style="height: 700px; border: 1px solid #eee">
        <!--侧边栏-->
        <component-sidebar></component-sidebar>

        <el-container>
            <!--标题-->
            <el-header style="text-align: right; font-size: 12px">
                <h1 id="pageTitle">图书管理系统</h1>
            </el-header>

            <!--主体-->
            <el-main>
                <!--搜索表单-->
                <el-form id="employeeSearchForm" :inline="true" :model="formInline" class="demo-form-inline">
                    <el-form-item label="姓氏：">
                        <el-input v-model="formInline.surname" placeholder="姓氏"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号：">
                        <el-input v-model="formInline.phone" placeholder="手机号"></el-input>
                    </el-form-item>
                    <el-form-item label="性别：">
                        <el-select v-model="formInline.sex" placeholder="性别">
                            <el-option label="男" value="男"></el-option>
                            <el-option label="女" value="女"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="身份证号：">
                        <el-input v-model="formInline.idNumber" placeholder="身份证号"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="searchEmployee">查询</el-button>
                    </el-form-item>
                    <el-button id="insertBtn" type="primary" icon="el-icon-plus" circle
                               @click="addDialogFormVisible = true">
                    </el-button>
                </el-form>
                <!--数据表格-->
                <el-table
                        :data="tableData"
                        border
                        style="width: 100%"
                        align="center"
                        id="table">

                    <el-table-column
                            prop="surname"
                            label="称呼"
                            align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.sex === '女'">{{ scope.row.surname }}女士</span>
                            <span v-else>{{ scope.row.surname }}先生</span>
                        </template>

                    </el-table-column>
                    <el-table-column
                            prop="phone"
                            label="电话号码"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="idNumber"
                            label="身份证号"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="createTime"
                            label="创建时间"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            fixed="right"
                            label="操作"
                            align="center">
                        <template slot-scope="scope">
                            <el-button @click="clickUpdate(scope.row)" type="text" size="small">编辑</el-button>
                            <el-button @click="clickDelete(scope.row)" type="text" size="small" style="color: red">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页条-->
                <div class="block">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="pageNation.currentPage"
                            :page-sizes="[5, 10, 20, 30]"
                            :page-size="pageNation.pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="pageNation.total">
                    </el-pagination>
                </div>

                <!--添加客户弹窗-->
                <el-dialog title="添加客户" :visible.sync="addDialogFormVisible">
                    <el-form :model="addUserForm">

                        <el-form-item label="姓氏:"
                                      prop="surname">
                            <el-input v-model="addUserForm.surname"
                                      placeholder="请填写姓氏"
                                      maxlength="20"/>
                        </el-form-item>
                        <el-form-item label="性别："
                                      prop="price">
                            <el-select v-model="addUserForm.sex" placeholder="请选择性别">
                                <el-option label="男" value="男"></el-option>
                                <el-option label="女" value="女"></el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="电话号码:"
                                      prop="phone">
                            <el-input v-model="addUserForm.phone"
                                      placeholder="请填写电话号码"
                                      maxlength="20"/>
                        </el-form-item>

                        <el-form-item label="身份证号:"
                                      prop="idNumber">
                            <el-input v-model="addUserForm.idNumber"
                                      placeholder="请填写身份证号"/>
                        </el-form-item>

                        <el-form-item align="center">
                            <el-button type="primary" @click="insertUser">提交</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>

                <!--修改客户弹窗-->
                <el-dialog title="修改客户信息" :visible.sync="updateDialogFormVisible">
                    <el-form :model="updateUserForm">
                        <el-form-item label="姓氏:"
                                      prop="surname">
                            <el-input v-model="updateUserForm.surname"
                                      placeholder="请填写姓氏"
                                      maxlength="20"/>
                        </el-form-item>
                        <el-form-item label="性别："
                                      prop="price">
                            <el-select v-model="updateUserForm.sex" placeholder="请选择性别">
                                <el-option label="男" value="男"></el-option>
                                <el-option label="女" value="女"></el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="电话号码:"
                                      prop="phone">
                            <el-input v-model="updateUserForm.phone"
                                      placeholder="请填写电话号码"
                                      maxlength="20"/>
                        </el-form-item>

                        <el-form-item label="身份证号:"
                                      prop="idNumber">
                            <el-input v-model="updateUserForm.idNumber"
                                      placeholder="请填写身份证号"/>
                        </el-form-item>

                        <el-form-item align="center">
                            <el-button type="primary" @click="updateUser">提交</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>
            </el-main>
        </el-container>

    </el-container>
</template>

<style>
.el-header {
    background-color: #B3C0D1;
    color: #333;
    line-height: 60px;
}

#pageTitle {
    margin: 0;
    font-size: 40px;
    text-align: center;
}

#insertBtn {
    display: flex;
    float: right;
}

.cover-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}

.cover {
    width: 100px;
    height: 100px;
    display: block;
}

.cover-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.cover-uploader .el-upload:hover {
    border-color: #409eff;
}

</style>

<script>
import ComponentSidebar from "@/components/ComponentSidebar.vue";
import axios from "axios";

const sendRequestMethod = {
    //发请求查询客户列表
    listUser(pageNation, formInline) {
        let url = "/user/admin/user/page";
        url += ("?page=" + pageNation.currentPage + "&pageSize=" + pageNation.pageSize);

        if (formInline.surname != null && formInline.surname !== "") {
            url += "&surname=" + formInline.surname;
        }
        if (formInline.phone != null && formInline.phone !== "") {
            url += "&phone=" + formInline.phone;
        }
        if (formInline.sex != null && formInline.sex !== "") {
            url += "&sex=" + formInline.sex;
        }
        if (formInline.idNumber != null && formInline.idNumber !== "") {
            url += "&idNumber=" + formInline.idNumber;
        }
        try {
            //console.log("请求url：" + url);
            axios.get(url, {
                headers: {
                    'token': sessionStorage.getItem("token")
                }
            }).then(
                    (res) => {
                        this.tableData = res.data.data.records;
                        this.pageNation.total = res.data.data.total;
                    }
            ).catch(
                    (err) => {
                        if (err.response.status === 401) {
                            //跳转登录页面
                            this.$router.push("/login");
                        } else {
                            this.openNotification("error", "错误", "查询客户失败");
                        }
                    }
            );
        } catch (error) {
            // 捕获OS错误或其他异常
            //console.error('Caught an error:', error);
            this.openNotification("error", "错误", "查询图书失败!");
        }
    },
}

export default {
    components: {ComponentSidebar},
    data() {
        const item = {
            id: '',
            surname: '',
            phone: '',
            sex: '',
            idNumber: '',
            createTime: '',
        };
        return {
            // 表格数据
            tableData: Array(0).fill(item),
            // 搜索表单数据
            formInline: {
                surname: null,
                phone: null,
                sex: null,
                idNumber: null,
            },
            //分页条属性
            pageNation: {
                currentPage: 1,
                pageSize: 5,
                total: 0
            },
            // 弹窗
            dialogTableVisible: false,
            addDialogFormVisible: false,
            updateDialogFormVisible: false,
            // 添加客户弹窗表单数据
            addUserForm: {
                phone: '',
                sex: '',
                surname: '',
                idNumber: ''
            },
            //修改客户弹窗表单数据
            updateUserForm: {
                phone: '',
                sex: '',
                surname: '',
                idNumber: '',
                row: ''
            },

            formLabelWidth: '120px',
            imageUrl: '',
        }
    },


    methods: {
        ...sendRequestMethod,

        //点击查询按钮
        searchEmployee() {
            this.listUser(this.pageNation, this.formInline);
        },

        //分页条pageSize改变
        handleSizeChange(val) {
            this.pageNation.pageSize = val;
            this.listUser(this.pageNation, this.formInline);
        },

        //分页条currentPage改变
        handleCurrentChange(val) {
            this.pageNation.currentPage = val;
            this.listUser(this.pageNation, this.formInline);
        },

        //弹出通知
        openNotification(type, title, message) {
            if (type === "error") {
                this.$notify.error({
                    title: title,
                    message: message
                });
            }

            if (type === "info") {
                this.$notify.info({
                    title: title,
                    message: message
                });
            }

            if (type === "warning") {
                this.$notify.warning({
                    title: title,
                    message: message
                });
            }

            if (type === "success") {
                this.$notify.success({
                    title: title,
                    message: message
                });
            }


        },

        //点击删除按钮
        clickDelete(row) {
            let url = "/user/admin/user?id=" + row.id;
            axios.delete(url, {
                headers: {
                    'token': sessionStorage.getItem("token")
                }
            }).then(
                    () => {
                        this.openNotification("success", "成功", "删除客户成功");
                        this.listUser(this.pageNation, this.formInline);
                    }
            ).catch(
                    (err) => {
                        console.error(err);
                        this.openNotification("error", "错误", "删除客户失败");
                    }
            );
        },

        //修改客户按钮点击
        clickUpdate(row) {
            this.updateDialogFormVisible = true;
            this.updateUserForm.row = row;
            //根据id查询客户信息
            let url = "/user/admin/user/" + row.id;
            axios.get(url, {
                headers: {
                    'token': sessionStorage.getItem("token")
                }
            }).then(
                    (res) => {
                        // console.log(res);
                        this.updateUserForm.surname = res.data.data.surname;
                        this.updateUserForm.phone = res.data.data.phone;
                        this.updateUserForm.sex = res.data.data.sex;
                    }
            ).catch(
                    (err) => {
                        console.error(err);
                        this.openNotification("error", "错误", "查询客户失败");
                    }
            );
        },

        //修改客户提交
        updateUser() {
            let url = "/user/admin/user";
            //console.log(this.updateUserForm);
            axios.put(url, {
                id: this.updateUserForm.row.id === '' ? null : this.updateUserForm.row.id,
                surname: this.updateUserForm.surname === '' ? null : this.updateUserForm.surname,
                sex: this.updateUserForm.sex === '' ? null : this.updateUserForm.sex,
                phone: this.updateUserForm.phone === '' ? null : this.updateUserForm.phone,
                idNumber: this.updateUserForm.idNumber === '' ? null : this.updateUserForm.idNumber,
            }, {
                headers: {
                    'token': sessionStorage.getItem("token"),
                    'Content-Type': 'application/json'
                }
            }).then(
                    () => {
                        this.openNotification("success", "成功", "修改成功");
                        this.updateDialogFormVisible = false;
                        this.listUser(this.pageNation, this.formInline);
                    }
            ).catch(
                    (err) => {
                        console.error(err);
                        this.openNotification("error", "错误", "修改失败");
                    }
            );
        },

        //添加客户弹窗提交
        insertUser() {
            let url = "/user/admin/user";
            axios.post(url, this.addUserForm, {
                headers: {
                    'token': sessionStorage.getItem("token"),
                    'Content-Type': 'application/json'
                }
            }).then(
                    () => {
                        this.openNotification("success", "成功", "添加客户成功");
                        this.addDialogFormVisible = false;
                        this.listUser(this.pageNation, this.formInline);
                        this.addUserForm.phone = '';
                        this.addUserForm.sex = '';
                        this.addUserForm.surname = '';
                        this.addUserForm.idNumber = '';
                    }
            ).catch(
                    (err) => {
                        console.error(err);
                        this.openNotification("error", "错误", "添加客户失败");
                    }
            );
        }
    },

    mounted() {
        try {
            this.listUser(this.pageNation, this.formInline);
        } catch (e) {
            console.error(e);
            this.openNotification("error", "错误", "前端错误")
        }
    }
};
</script>
